using System;
using Light;
using Light.Assets.Scripts.EventManager;
using Refresh;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[Serializable]
public class Item : MonoBehaviour, IRefresh, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    [SerializeField] private MyItem _item;
    [SerializeField] private Button _button;
    [SerializeField] private Text _itemName, _itStatus;
    [SerializeField] private Image _itImage;

    [SerializeField] private Color targetColor;
    [SerializeField] private Color originColor;

    private Image _image; //�������е�ͼƬ


    private RectTransform _rectransfrom;

    private AbstractItemSO data;


    /// <summary>
    ///     ��ʱ��ק����
    /// </summary>
    private GameObject itemOnDrag;

    private GoodsController parentPanel;


    //�涨item����������
    private FunctionBarContentType type;

    private void Awake()
    {
        _button = GetComponent<Button>();

        _image = GetComponent<Image>();

        _rectransfrom = GetComponent<RectTransform>();
    }

    private void OnEnable()
    {
        //�������ʾItem����
        _button.onClick.AddListener(ButtonClickSubmit);
    }

    private void OnDisable()
    {
        //�Ƴ����м���
        _button.onClick.RemoveAllListeners();
    }

    /// <summary>
    ///     ��ʼ��ק
    /// </summary>
    /// <param name="eventData"></param>
    /// <exception cref="System.NotImplementedException"></exception>
    public void OnBeginDrag(PointerEventData eventData)
    {
        itemOnDrag = Instantiate(gameObject, parentPanel.transform);
        itemOnDrag.transform.position = eventData.position;
    }

    /// <summary>
    ///     ��ק��
    /// </summary>
    /// <param name="eventData"></param>
    /// <exception cref="System.NotImplementedException"></exception>
    void IDragHandler.OnDrag(PointerEventData eventData)
    {
        itemOnDrag.transform.position = eventData.position;
    }

    /// <summary>
    ///     ������ק
    /// </summary>
    /// <param name="eventData"></param>
    /// <exception cref="System.NotImplementedException"></exception>
    void IEndDragHandler.OnEndDrag(PointerEventData eventData)
    {
        // if (eventData.position.x.InRange(parentPanel.itemEuqipUIArea.Item1.x, parentPanel.itemEuqipUIArea.Item1.y)
        //     && eventData.position.y.InRange(parentPanel.itemEuqipUIArea.Item2.x, parentPanel.itemEuqipUIArea.Item2.y))
        // {
        //     Debug.Log("����װ��Item"); 
        //     EquipItem();
        // }
        // else if (eventData.position.x.InRange(parentPanel.itemContentUIArea.Item1.x, parentPanel.itemContentUIArea.Item1.y)
        //     && eventData.position.y.InRange(parentPanel.itemContentUIArea.Item2.x, parentPanel.itemContentUIArea.Item2.y))
        // {
        //     Debug.Log("����ж��Item");
        //     RelieveItem();
        // }
        // else
        // {
        //     Debug.Log("���Զ���Item");
        //     DestoryItem();
        // }
        //
        // Destroy(itemOnDrag);
    }


    public void Refresh<T>(T data) where T : RefreshData<T>, new()
    {
        var info = data as ItemRefreshData;
        if (info.type == FunctionBarOperationType.Delete)
        {
            Destroy(this);
        }
        else
        {
            this.data = info.data;
            _itemName.text = this.data.itemName;
        }
    }

    public void Init(GoodsController panel, FunctionBarContentType contentType)
    {
        parentPanel = panel;
        type = contentType;

        //Vector3 tempScale =  _rectransfrom.GetScale();
        //tempScale.y = 0.1f;
        //_rectransfrom.SetScale(tempScale);
    }

    public void ButtonClickSubmit()
    {
        //�������ݴ��ݹ�ȥ
        var info = ItemDetailRefreshData.Get();

        //����ȫ�����ݹ�ȥ
        info.data = data;

        parentPanel.itemParamsChangeUI.Refresh(info);
    }

    /// <summary>
    ///     �������ò�
    /// </summary>
    public void Move()
    {
        //����ʽ���
        //_rectransfrom.DOScaleY(1, 0.3f);
    }

    /// <summary>
    ///     װ��
    /// </summary>
    private void EquipItem()
    {
        if (type == FunctionBarContentType.GoodsItemEuqip) return;
        var evt = GoodsBarSwtichItemEvent.Get();
        evt.item = data;
        evt.originType = FunctionBarContentType.GoodsScrollView;
        evt.targetType = FunctionBarContentType.GoodsItemEuqip;

        EventManager.SendEvent(evt);
    }

    /// <summary>
    ///     ���
    /// </summary>
    private void RelieveItem()
    {
        if (type == FunctionBarContentType.GoodsScrollView) return;

        var evt = GoodsBarSwtichItemEvent.Get();
        evt.item = data;
        evt.originType = FunctionBarContentType.GoodsItemEuqip;
        evt.targetType = FunctionBarContentType.GoodsScrollView;

        EventManager.SendEvent(evt);

        evt.Dispose();
    }

    /// <summary>
    ///     ��������ʱ//TODO
    /// </summary>
    private void DestoryItem()
    {
        //�Ӻ�̨�������
        //remove
        using var evt = GoodsBarDeleteItemEvent.Get();
        evt.item = data;
        EventManager.SendEvent(evt);


        parentPanel.DeleteGameObject(data);
        Destroy(gameObject);
    }
}